package net.csdn.business.webhook.controller;

import com.alibaba.fastjson2.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.annotation.Inner;
import net.csdn.business.common.domain.request.webhook.WebhookNotifyMessage;
import net.csdn.business.common.domain.vo.WebHookEvent;
import net.csdn.business.webhook.config.WebHookEventConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WebHookEventTestController
 * @Description
 * @Author zhangyl
 * @Date 2023/4/26 11:02
 */
@RestController
@RequestMapping("/api/v1/webhook/event/test")
@Slf4j
public class WebHookEventTestController {

    @Autowired
    private  ProducerBean aliProducer;

    @Value("${ali.mq.webhook.topic:WEB_HOOK_EVENT}")
    private String webhookTopic;


    /**
     * 查询组织、项目、issue的webhook的事件
     */
    @PostMapping("send")
    public Integer getEvent(@RequestBody WebhookNotifyMessage param){
        this.sendMsg(JSON.toJSONString(param),param.getEventType());
        return 1;
    }


    private void sendMsg(String content, String tag) {
        Message message = new Message(webhookTopic, tag, content.getBytes(StandardCharsets.UTF_8));
        SendResult result = aliProducer.send(message);
        log.info("==== tcp方式发送MQ消息,topic={},tag={},content={},msgId={}", webhookTopic, tag, content, result.getMessageId());
    }


}